package com.woniuxy.controller;



import com.woniuxy.model.dto.UserLoginDto;
import com.woniuxy.model.entity.Customers;
import com.woniuxy.service.CustomersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户登录")
public class UserController {

    @Autowired
    private CustomersService customersService;


    @GetMapping("/isLogged")
    @ApiOperation("判断用户是否登录")
    public ResponseEntity<?> isLogged(@RequestHeader(required = false, name = "Authorization") String authorization) {
        log.info("用户请求头中携带的Token是：{}", authorization);
        Customers customers = customersService.isLogged(authorization);
        return ResponseEntity.ok(customers);
    }

    /**
     * 发短信登录或注册
     */
    @PostMapping("/smsLogin")
    @ApiOperation("发短信登录或注册")
    public ResponseEntity<?> smsLogin(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request) {
        Customers customers = customersService.smsLogin(userLoginDto, request);
        return ResponseEntity.ok(customers);
    }


    /**
     * 退出
     * @param token
     * @param response
     * @return
     */
    @DeleteMapping("/logout")
    @ApiOperation("退出")
    public ResponseEntity<String> logout(@RequestHeader("Authorization") String token, HttpServletResponse response) {
      customersService.logout(token, response);
        return ResponseEntity.ok("退出成功");
    }



    /**
     * 手机号发短信
     */
    @GetMapping("/sendSms/{telephone}")
    @ApiOperation("手机号发短信")
    public ResponseEntity<String> sendSms(@PathVariable String telephone) {
        String resp = customersService.sendSms(telephone);
        return ResponseEntity.ok(resp);
    }





}